package com.fh.UDP;

import java.io.IOException;
import java.net.*;

/**
 * @author 方小勤
 * @version 1.0~
 */
public class UDPSenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] data = "hello 明天去吃火锅".getBytes();
        DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("192.168.110.10"),9999);
        socket.send(packet);
        //==============接收 A 端回复的数据=================
        byte[] buf = new byte[1024];
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        //把packet拆包，取出数据并显示
        int length = packet.getLength();//实际接收到的数据字节长度
        byte[] data1 = packet.getData();//接收到数据
        String s = new String(data1, 0, length);
        System.out.println(s);
        //关闭资源
        socket.close();
        System.out.println("B 端退出");
    }
}
